package commons.base.netty.http;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

public class Example {

	public static void main(String[] args) {
		HttpServer httpServer = new HttpServer(9000);
		
		httpServer.addServlet("/", new DefaultServlet());
		httpServer.addServlet("/hello", new WelcomeServlet());
		
		httpServer.start();
	}

}

class DefaultServlet extends NettyHttpServlet{

	@Override
	public void init() throws Exception {
		System.err.println("init");
		
	}

	@Override
	public Object doService(Map<String, String> params) throws Exception {
		Path path = Paths.get(DefaultServlet.class.getResource("/index.html").toURI());
		
		return new String(Files.readAllBytes(path), "UTF-8");
	}

	@Override
	public void destroy() {
		System.err.println("destroy");
		
	}
	
}
class WelcomeServlet extends NettyHttpServlet{
	
	@Override
	public void init() throws Exception {
		System.err.println("init");
		
	}
	
	@Override
	public Object doService(Map<String, String> params) throws Exception {
		System.err.println("doService");
		return "hello world";
	}
	
	@Override
	public void destroy() {
		System.err.println("destroy");
		
	}
	
}
